/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.babble.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.babble.blog.conversion.BlogConversionMapper;
import com.babble.blog.dto.QueryBlogDTO;
import com.babble.blog.entity.Blog;
import com.babble.blog.mapper.BlogMapper;
import com.babble.blog.service.IBlogCategoryService;
import com.babble.blog.service.IBlogService;
import com.babble.blog.vo.BlogSaveVO;
import com.babble.blog.vo.BlogUpdVO;
import com.babble.blog.vo.BlogVO;
import com.babble.manage.entity.Img;
import com.babble.manage.feign.IImgClient;
import com.babble.subitem.feign.ITagClient;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springblade.common.cache.CacheNames;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.CodeMsg;
import org.springblade.common.exception.MsgException;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.RedisUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 博客文章表 服务实现类
 *
 * @author Blade
 * @since 2021-11-19
 */
@Service
@RequiredArgsConstructor
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {
	public final RedisUtil redisUtil;
	public final RabbitTemplate rabbit;
	public final IBlogCategoryService iBlogCategoryService;
	public final ITagClient tagClient;
	public final IImgClient imgClient;
	public final BlogConversionMapper blogConversionMapper;
	@Override
	public IPage<BlogVO> selectBlogPage(IPage<BlogVO> page, BlogVO blog) {
		return page.setRecords(baseMapper.selectBlogPage(page, blog));
	}

	@Override
	public void saveBlog(BlogSaveVO blogsaveVO) throws MsgException, MsgException, MsgException {
		Blog blog = blogConversionMapper.blogSaveVOToBLog(blogsaveVO);
		blog.setUserId(SecureUtil.getUser().getUserId().toString());
		//判断是否存在此分类
		iBlogCategoryService.info(blog.getBlogCategoryId());
		//Tagfeign判断此标签是否存在
		tagClient.info(blog.getTagId());
		//远程feign判断是否存在此图片
		R<Img> img = imgClient.getImg(blog.getImgId());
		if(img.getData()==null){
			//TODO (cyq)添加异常信息
			throw new MsgException(1,"暂未添加");
		}
		String json = JSON.toJSONString(blog);
		//TODO (cyq)从es中拿到博客信息,判断标题是否存在

		rabbit.convertAndSend(CommonConstant.RABBIT_BLOG_EXCHANGE, CommonConstant.RABBID_BLOG_SAVE_KEY, json);
	}

	@Override
	public List<BlogVO> list(QueryBlogDTO queryBlogDTO) {
		//TODO (cyq)归纳页
		return null;
	}

	@Override
	public void clickBlog(String id) throws MsgException {
		//判断是否存在此博客
		Blog blog = getBlogIsId(id);
		if (blog == null) {
			throw new MsgException(CodeMsg.BLOG_EXISTS_ERROR.getCode(), CodeMsg.BLOG_EXISTS_ERROR.getMsg());
		}
		Long userId = SecureUtil.getUser().getUserId();
		Object json = redisUtil.get(CacheNames.BLOG_BLOG_CLICK + userId + ":" + id);
		if (json == null) {
			blog.setClickCount(blog.getClickCount() + 1);
			this.updateById(blog);
			redisUtil.set(CacheNames.BLOG_BLOG_CLICK + userId + ":" + id, "0", 60, TimeUnit.SECONDS);
			//TODO (cyq)更新cls数据
			return;
		}
		throw new MsgException(CodeMsg.BLOG_CLICK_EXISTS_ERROR.getCode(), CodeMsg.BLOG_CLICK_EXISTS_ERROR.getMsg());
	}

	@Override
	public List<BlogVO> listById(String id) throws MsgException {
		//TODO (cyq)从es中拿数据获取默认先抛异常
		if(false){
			throw new MsgException(CodeMsg.BLOG_EXISTS_ERROR.getCode(),CodeMsg.BLOG_EXISTS_ERROR.getMsg());
		}
		return null;
	}

	@Override
	public void updBlog(BlogUpdVO blogUpdVO) throws MsgException, MsgException {
		Blog blog = blogConversionMapper.blogUpdVOToBLog(blogUpdVO);
		//判断文章存在不
		getBlogIsId(blog.getId());
		String blogCategoryId = blog.getBlogCategoryId();
			if(blogCategoryId!= null && !"".equals(blogCategoryId)){
			//查找是否有此文章分类
			iBlogCategoryService.info(blogCategoryId);
		}
		String imgId = blog.getImgId();
		if(imgId!=null && !"".equals(imgId)){
			//远程查找是否存在此图片
			R<Img> img = imgClient.getImg(blogCategoryId);
			if(img.getData()==null){
				//TODO (cyq)添加异常信息
				throw new MsgException(1,"暂未添加");
			}
		}
		this.updateById(blog);
		//TODO (cyq)更新es数据
	}

	@Override
	public void del(String id) {
		getBlogIsId(id);
		this.removeById(id);
		//TODO (cyq)删除es中的此文章
	}

	public Blog getBlogIsId(String id) {
		//TODO (cyq)使用els获取博客文章没有抛异常
		return null;
	}
}
